home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / Document / DocumentIcon.m < prev    next >
Text File  |  1995-06-12  |  6KB  |  255 lines

  1. /* DocumentIcon.m:
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  *
  6.  * Written by Adam Hertz
  7.  * Modified: Mike Riggs, Mai Nguyen
  8.  *
  9.  */
  10.  
  11. #import <dbkit/dbkit.h>
  12. #import "DocumentIcon.h"
  13.  
  14. @implementation DocumentIcon
  15. - initFrame:(const NXRect *)frameRect
  16. {
  17.     const char *dragTypes;
  18.     [super initFrame:frameRect];
  19.     dragTypes = NXFilenamePboardType;
  20.     [self registerForDraggedTypes:&dragTypes count: 1];
  21.     return self;
  22. }
  23.  
  24. - free
  25. {
  26.     [self unregisterDraggedTypes];
  27.     return [super free];
  28. }
  29.  
  30.  
  31. - drawSelf:(const NXRect *)rects :(int)rectCount
  32. {
  33.     NXRect imageRect;
  34.     NXSize imageSize;
  35.     NXPoint drawOrigin;
  36.     
  37.     NXDrawGrayBezel(&bounds, rects);
  38.     imageRect.origin.x    = bounds.origin.x    + 2;
  39.     imageRect.origin.y    = bounds.origin.y    + 2;
  40.     imageRect.size.width  = bounds.size.width  - 4;
  41.     imageRect.size.height = bounds.size.height - 4;
  42.  
  43.     PSgsave();
  44.     NXRectClip(&imageRect);
  45.     if (image != nil) {
  46.     [image getSize:&imageSize];
  47.     drawOrigin = imageRect.origin;
  48.     drawOrigin.x += (imageRect.size.width - imageSize.width) / 2;
  49.     drawOrigin.y += (imageRect.size.height - imageSize.height) / 2;
  50.     [image composite:NX_SOVER toPoint:&drawOrigin];
  51.     }
  52.     PSgrestore();
  53.  
  54.     return self;
  55. }
  56.  
  57. #define mask (NX_LMOUSEUPMASK|NX_LMOUSEDRAGGEDMASK)
  58. #define Shift(e) (e->flags&(NX_NEXTLSHIFTKEYMASK|NX_NEXTRSHIFTKEYMASK))
  59. #ifndef abs
  60. #define abs(x) (((x)<0)? -(x) : (x))
  61. #endif
  62.  
  63. int
  64. mouseMoved(NXPoint *o, int n) { /* true if mouse moves > n pixels from 'o' */
  65.     NXEvent *e;
  66.     NXPoint p;
  67.     float d;
  68.     do {
  69.         e = [NXApp getNextEvent:mask];
  70.         p = e->location;
  71.         d = abs(p.x-o->x);
  72.         if (d < abs(p.y-o->y)) d = abs(p.y-o->y);
  73.     } while (e->type != NX_LMOUSEUP && d<n);
  74.     *o = p;
  75.     return e->type != NX_LMOUSEUP;
  76. }
  77.  
  78. - mouseDown: (NXEvent *) e
  79. {
  80.     NXPoint    hitPoint;
  81.     int        oldMask;
  82.     NXEvent    saveEvent;
  83.     Pasteboard    *dragPasteboard;
  84.  
  85.         /* Call the workspace protocol to open the file at a double-click */
  86.     if( e->data.mouse.click > 1
  87.         && filename && *filename) {
  88.     [[Application workspace] openFile: filename];
  89.     }
  90.  
  91.     oldMask = [window eventMask];
  92.     [window setEventMask:(oldMask|mask)];
  93.     saveEvent = *e;
  94.     hitPoint = e->location;
  95.  
  96.     if (mouseMoved(&hitPoint,4)) {
  97.     NXRect    dragRect;
  98.     
  99.     if( filename && *filename) {
  100.         [self convertPoint: &hitPoint fromView: nil];
  101.         [self getBounds: &dragRect];
  102.         
  103.         dragPasteboard = [Pasteboard newName: NXDragPboard];
  104.         [dragPasteboard declareTypes: &NXFilenamePboardType num: 1 owner: nil];
  105.         [dragPasteboard writeType: NXFilenamePboard data: filename
  106.             length: strlen( filename)];
  107.         
  108.         [self dragImage:image at:&(dragRect.origin) offset:&hitPoint
  109.             event:&saveEvent pasteboard:dragPasteboard
  110.             source:self slideBack: YES];
  111.         }
  112.     }
  113.  
  114.     return [super mouseDown: e];
  115. }
  116.  
  117. - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag
  118. {
  119.     return NX_DragOperationCopy;
  120. }
  121.  
  122. - setTarget:anObject            {target = anObject; return self;}
  123. - setAction:(SEL)aSelector        {action = aSelector; return self;}
  124. - target                {return target;}
  125. - (SEL)action                {return action;}
  126.  
  127. - (const char *) stringValue
  128. {
  129.     return filename;
  130. }
  131.  
  132. - setStringValue:(const char *)aString
  133. {
  134.     int                 len;
  135.  
  136.     if (filename)
  137.     NXZoneFree([self zone], (void *)filename);
  138.     filename = NULL;
  139.     if (aString) {
  140.     filename = NXCopyStringBufferFromZone(aString,[self zone]);
  141.     if (filename && *filename && aString) {
  142.         for (len = strlen(filename) - 1; len > 0; len--) {
  143.         if (filename[len] == ' ') {
  144.             filename[len] = '\0';
  145.         } else
  146.             break;
  147.         }
  148.         image = [[Application workspace] getIconForFile:filename];
  149.         [self display];
  150.     }
  151.     }
  152.     return self;
  153. }
  154.  
  155.  
  156.  
  157. /**********************************************************************/
  158. /*                            Archiving                               */
  159.  
  160. - read:(NXTypedStream *)stream
  161. {
  162.     const char *dragTypes = NXFilenamePboardType;
  163.     [super read: stream];
  164.     [self registerForDraggedTypes:&dragTypes count: 1];
  165.     return self;
  166. }
  167.  
  168. /**********************************************************************/
  169. /*                            Dragging                                */
  170.  
  171. - (NXDragOperation) draggingEntered: sender
  172. {
  173.     id dragPasteboard;
  174.     char *path;
  175.     int length;
  176.  
  177.     dragPasteboard = [sender draggingPasteboard];
  178.  
  179.     if( ([dragPasteboard readType:NXFilenamePboardType
  180.         data:&path length:&length])
  181.         && (path && *path && *path == '/')) {
  182.     return  NX_DragOperationCopy;
  183.     }
  184.  
  185.     return NX_DragOperationNone;
  186. }
  187.  
  188.  
  189.  
  190. - (BOOL)performDragOperation:(id <NXDraggingInfo>)sender
  191. {
  192.     char *path;
  193.     int length;
  194.     id dragPasteboard;
  195.  
  196.     dragPasteboard = [sender draggingPasteboard];
  197.  
  198.     if ([dragPasteboard readType:NXFilenamePboardType
  199.         data:&path length:&length]) {
  200.  
  201.     char p[MAXPATHLEN];
  202.     strncpy( p, path, length);
  203.     p[length] = '\0';
  204.     if( filename) NXZoneFree( [self zone], (void *)filename);
  205.     filename = NULL;
  206.     filename = NXCopyStringBuffer(p);
  207.     image = [sender draggedImageCopy];
  208.     [dragPasteboard deallocatePasteboardData:path length:length];
  209.     
  210.     [self display];
  211.     if( target != nil)
  212.         [self sendAction:action to:target];
  213.         
  214.     return YES;
  215.     }
  216.  
  217.     return NO;
  218. }
  219.  
  220.  
  221. - draggingExited:sender
  222. {
  223.     return self;
  224. }
  225.  
  226. /**********************************************************************/
  227. /*                 DBCustomAssociation Implementation                 */
  228. - updateForAssociation:association
  229. {
  230.     id                  val = [[DBValue alloc] init];
  231.  
  232.     [[[association fetchGroup] recordList] getValue:val
  233.      forProperty:[association expression]
  234.      at:[[association fetchGroup] currentRecord]];
  235.  
  236.     [self setStringValue:[val stringValue]];
  237.     [val free];
  238.     return self;
  239. }
  240.  
  241.  
  242. - associationContentsDidChange:association
  243. {
  244.     return [self updateForAssociation: association];
  245. }
  246.  
  247. - associationSelectionDidChange:association
  248. {
  249.     return [self updateForAssociation: association];
  250. }
  251.  
  252. @end
  253.  
  254.  
  255.